//
//  SevendayViewController.m
//  B_WatchHome
//
//  Created by dllo on 15/12/24.
//  Copyright © 2015年 DHF. All rights reserved.
//

#import "SevendayViewController.h"
#import "SevendayModel.h"
#import "AppTools.h"
#import <MJRefresh.h>
#import "ZeroTableViewCell.h"
#import "ThreeTableViewCell.h"
#import "DataViewController.h"
#import "ThreeViewController.h"
#import <UIImage+GIF.h>

@interface SevendayViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, assign)NSInteger i;
@property (nonatomic, strong)UITableView *myTableView;

@end

@implementation SevendayViewController

- (void)viewWillAppear:(BOOL)animated
{
    [self loadNewData];
    
    [self.myTableView reloadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"七日热点";
    
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    
    self.myTableView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.myTableView];
    
    [self.myTableView registerClass:[ZeroTableViewCell class] forCellReuseIdentifier:@"one"];
    [self.myTableView registerClass:[ThreeTableViewCell class] forCellReuseIdentifier:@"three"];
    
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    
    self.bigArray = [NSMutableArray array];
    
    // 上下拉刷新
    [self addFooterRefresh];
    
//    [self loadNewData];
    
    
    self.i = 0;
    
    MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    
    NSString *name = @"header.gif";
    NSString *filePath = [[NSBundle bundleWithPath:[[NSBundle mainBundle] bundlePath]] pathForResource:name ofType:nil];
    NSData *imageData = [NSData dataWithContentsOfFile:filePath];
    UIImage *image = [UIImage sd_animatedGIFWithData:imageData];
    
    NSArray *arr = [NSArray arrayWithObjects:image, nil];
    
    [header setImages:arr forState:MJRefreshStateIdle];
    
    [header setImages:arr forState:MJRefreshStatePulling];
    
    [header setImages:arr forState:MJRefreshStateRefreshing];
    
    self.myTableView.mj_header = header;
}

- (void)loadNewData
{
    NSLog(@"下拉");
    
    self.i = 1;
    
    //  重新请求数据
    [self getAFNetWorkingData];
    
    [self.myTableView.mj_header beginRefreshing];
}


//- (void)addHeaderRefresh
//{
//    self.myTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
//       
//        NSLog(@"下拉");
//        self.i = 0;
//        // 重新请求数据
//        [self getAFNetWorkingData];
//        
//    }];
//    [self.myTableView.mj_header beginRefreshing];
//}

- (void)addFooterRefresh
{
    self.myTableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        
        NSLog(@"上拉");
        self.i++;
        [self getAFNetWorkingData];
        
    }];
    
}


- (void)getAFNetWorkingData
{
    NSString *urlStr = [NSString stringWithFormat:@"http:api.watchnews.app887.com/api/Articles.action?keyword=&npc=%ld&opc=20&type=行业资讯&uid=2564", self.i];
    
    if (self.i == 0) {
        
        // 删除拥有mod的array
        [AppTools getDataWithURL:urlStr Block:^(id result) {
           
            [self.bigArray removeAllObjects];
            NSMutableDictionary *biggestDic = result;
            NSMutableDictionary *dic1 = [biggestDic objectForKey:@"root"];
            NSMutableArray *array = [dic1 objectForKey:@"list"];
            
            for (NSMutableDictionary *dic3 in array) {
                SevendayModel *model = [[SevendayModel alloc] init];
                
                [model setValuesForKeysWithDictionary:dic3];
                
                [self.bigArray addObject:model];
                
            }
            NSLog(@"1111111111%ld", self.bigArray.count);
            // 刷新
            [self.myTableView reloadData];
            
            // 上下拉刷新结束
            [self.myTableView.mj_header endRefreshing];
            [self.myTableView.mj_footer endRefreshing];
            
        }];
    }else{
    
        [AppTools getDataWithURL:urlStr Block:^(id result) {
            
            NSMutableDictionary *biggestDic = result;
            
            NSMutableDictionary *dic1 = [biggestDic objectForKey:@"root"];
            
            NSMutableArray *array = [dic1 objectForKey:@"list"];
            
            for (NSMutableDictionary *dic3 in array) {
                SevendayModel *model = [[SevendayModel alloc] init];
                [model setValuesForKeysWithDictionary:dic3];
                
                [self.bigArray addObject:model];
            }
            
            [self.myTableView reloadData];
            
            // 上下拉刷新结束
            [self.myTableView.mj_header endRefreshing];
            [self.myTableView.mj_footer endRefreshing];
            
        }];
    
    }

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.bigArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SevendayModel *model = [self.bigArray objectAtIndex:indexPath.row];
    if (model.TYPESETTING == 0) {
        static NSString *cellIdentifier = @"one";
        
        ZeroTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        cell.model = model;
        return cell;
    }else{
        
        static NSString *cellIdentifier = @"three";
        
        ThreeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        
        cell.model = model;
        
        return cell;
        
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    SevendayModel *model = [self.bigArray objectAtIndex:indexPath.row];
    
    if (model.TYPESETTING == 0) {
        return 130;
    }else{
        return 150;
    }
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    SevendayModel *model = [self.bigArray objectAtIndex:indexPath.row];
    if (model.TYPESETTING == 0) {
        DataViewController *dVC = [[DataViewController alloc] init];
        dVC.model = model;
        [self.navigationController pushViewController:dVC animated:YES];
    }else{
        
        ThreeViewController *ThreeVC = [[ThreeViewController alloc] init];
        ThreeVC.model = model;
        
        [self presentViewController:ThreeVC animated:YES completion:^{
            
            
        }];
    
    }
    

}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
